package com.easymeeting.controller;

import com.easymeeting.annotation.GlobalInterceptor;
import com.easymeeting.entity.dto.SysSettingDto;
import com.easymeeting.entity.dto.TokenUserInfoDto;
import com.easymeeting.entity.po.UserInfo;
import com.easymeeting.entity.query.UserInfoQuery;
import com.easymeeting.entity.vo.CheckCodeVO;
import com.easymeeting.entity.vo.ResponseVO;
import com.easymeeting.entity.vo.SysSettingVO;
import com.easymeeting.entity.vo.UserInfoVO;
import com.easymeeting.exception.BusinessException;
import com.easymeeting.redis.RedisComponet;
import com.easymeeting.service.UserInfoService;
import com.easymeeting.utils.CopyTools;
import com.wf.captcha.ArithmeticCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.IOException;

/**
 * 用户信息 Controller
 */
@RestController
@RequestMapping("/account")
@Slf4j
public class AccountController extends ABaseController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RedisComponet redisComponet;

    @RequestMapping("/checkCode")
    public ResponseVO checkCode(UserInfoQuery query) {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
        String code = captcha.text();
        String base64 = captcha.toBase64();
        String checkcodekey = redisComponet.saveCheckCode(code);
        log.info("验证码：" + code);

        CheckCodeVO checkCodeVO = new CheckCodeVO();
        checkCodeVO.setCheckCodeKey(checkcodekey);
        checkCodeVO.setCheckCode(base64);
        return getSuccessResponseVO(checkCodeVO);
    }

    @RequestMapping("/register")
    public ResponseVO register(@NotEmpty String checkCodeKey,
                               @NotEmpty String email,
                               @NotEmpty @Size(max = 20) String password,
                               @NotEmpty @Size(max = 20) String nickName,
                               @NotEmpty String checkCode) {
        try {
            if (!checkCode.equalsIgnoreCase(redisComponet.getCheckCode(checkCodeKey))) {
                throw new BusinessException("验证码错误");
            }
            userInfoService.register(email, password, nickName);
            return getSuccessResponseVO(null);
        } finally {
            redisComponet.cleanCheckCode(checkCodeKey);
        }
    }

    @RequestMapping("/login")
    public ResponseVO register(@NotEmpty String checkCodeKey,
                               @NotEmpty String email,
                               @NotEmpty @Size(max = 20) String password,
                               @NotEmpty String checkCode) {
        try {
            if (!checkCode.equalsIgnoreCase(redisComponet.getCheckCode(checkCodeKey))) {
                throw new BusinessException("验证码错误");
            }
            UserInfoVO vo = userInfoService.login(email, password);
            return getSuccessResponseVO(vo);
        } finally {
            redisComponet.cleanCheckCode(checkCodeKey);
        }
    }

    @RequestMapping(value = "/getSysSetting")
    @GlobalInterceptor
    public ResponseVO getSysSetting() {
        SysSettingDto sysSettingDto = redisComponet.getSysSetting();
        return getSuccessResponseVO(CopyTools.copy(sysSettingDto, SysSettingVO.class));
    }

    @RequestMapping(value = "/updateUserInfo")
    @GlobalInterceptor
    public ResponseVO updateUserInfo(MultipartFile avatar, @NotEmpty String nickName, @NotNull Integer sex) throws IOException {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        UserInfo userInfo = new UserInfo();
        userInfo.setSex(sex);
        userInfo.setNickName(nickName);
        userInfo.setUserId(tokenUserInfoDto.getUserId());
        userInfoService.updateUserInfo(avatar, userInfo);
        return getSuccessResponseVO(null);
    }

    @RequestMapping(value = "/updatePassword")
    @GlobalInterceptor
    public ResponseVO updatePassword(@NotEmpty String oldPassword, @NotEmpty String password) {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        userInfoService.updatePassword(tokenUserInfoDto.getUserId(), oldPassword, password);
        return getSuccessResponseVO(null);
    }
    

}